package com.max.baselib.widget.toolbar

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.MenuRes
import androidx.annotation.StyleRes
import com.max.baselib.widget.toolbar.ToolbarConstants.Companion.def_back_navigationResId
import com.max.baselib.widget.toolbar.ToolbarConstants.Companion.def_bgColor_NaN
import com.max.baselib.widget.toolbar.ToolbarConstants.Companion.def_menuResId_NaN
import com.max.baselib.widget.toolbar.ToolbarConstants.Companion.def_titleStyle_NaN
import com.mxin.ui.R

/**
 *  Created by ：2020/7/27
 *  author : Administrator
 *
 */
abstract class ToolbarHelper<T:View> {

    internal var titleView: T

    constructor(view:View){
        if(isSelfView(view)){
            titleView = view as T
        }else{
            titleView = createView(view, R.style.toolbarStyle)
            attach(view, titleView)
        }
    }

    /**
     * 显示左侧图标（默认显示）
     */
    var showNavigation = true

    /**
     * 标题文字
     */
    var title : String? = null

    /**
     * 左侧图标
     */
    @DrawableRes
    var navigationIcon : Int = def_back_navigationResId
        get() = if(field != def_back_navigationResId) field
                else getConfig().navigationIcon

    /**
     * 左侧图标的点击事件
     */
    var navigationClickListener: View.OnClickListener? = null

    /**
     * 标题文字样式
     */
    var titleStyle:Int = def_titleStyle_NaN
        get() = when {
            field != def_titleStyle_NaN -> field
            getConfig().titleStyle != def_titleStyle_NaN -> getConfig().titleStyle
            else -> getDefaultTitleStyle()
        }

    /**
     * Toolbar 背景色
     */
    @StyleRes
    var bgColor :Int = def_bgColor_NaN
        get() = when {
            field != def_bgColor_NaN -> field
            getConfig().bgColor != def_bgColor_NaN -> getConfig().bgColor
            else -> R.color.colorPrimary
        }

    /**
     * 菜单资源
     */
    @MenuRes
    var menuResId:Int = def_menuResId_NaN


    protected abstract fun isSelfView(view:View):Boolean

    protected abstract fun createView(parent: View, @StyleRes styleResId: Int): T

    protected abstract fun setTitle(title: String?, titleStyle: Int)

    protected abstract fun getDefaultTitleStyle(): Int

    abstract fun build()

    companion object{

        private var config : ToolbarConfig ?= null


        /**
         *  view 有两种
         *  1. 自己在xml中定义了对应的titleView
         *  2. 传入viewStub或者ViewGroup，需要新建TitleView，然后关联view
         */
         fun <T:View> from(view: View, block:(ToolbarHelper<T>)->Unit) : T{
            return when(getConfig().toolbarType){
                ToolbarType.LeftTitle -> ToolbarHelperLeftTitle(view)
                ToolbarType.CenterTitle -> ToolbarHelperCenterTitle(view)
            }.apply {
                block(this as ToolbarHelper<T>)
                build()
            }.titleView as T
        }

        fun setConfig(tb_config: ToolbarConfig){
            config = tb_config
        }

        fun getConfig():ToolbarConfig{
            if(config==null){
                config = ToolbarConfig.defaultConfig()
            }
            return config!!
        }
    }

    fun attach(attachView: View, @LayoutRes layoutRes:Int){
        if(attachView is ViewStub){
            viewStub(attachView, layoutRes)
        }
        else if(attachView is ViewGroup){
            val view = LayoutInflater.from(attachView.context).inflate(layoutRes, attachView, false)
            attachView.addView(view)
        }else{
            throw IllegalAccessException("toolbarView attach error, attachView : ${attachView::class.java.name} , layoutRes : ${layoutRes}")
        }
    }

    fun attach(attachView:View, target:View){
        if(attachView is ViewStub){
            viewStub(attachView, target)
        }else if(attachView is ViewGroup){
            attachView.addView(createView(attachView, R.style.toolbarStyle))
        }else{
            throw IllegalAccessException("toolbarView attach error, attachView : ${attachView::class.java.name} , targetView : ${target::class.java.name}")
        }
    }

    private fun viewStub(viewSub: ViewStub, @LayoutRes layoutRes:Int): View{
        viewSub.layoutResource = layoutRes
        return viewSub.inflate()
    }

    private fun viewStub(viewSub: ViewStub, target: View): View{
        val parentView = viewSub.parent as ViewGroup
        // 获取view在父View的索引位置
        val index = parentView.indexOfChild(viewSub)
        //移除view
        parentView.removeViewAt(index)
        //转移LayoutParams
        val lp = viewSub.layoutParams
        //添加view
        parentView.addView(target, index, lp)
        return target
    }

}